import { MockMethod, Recordable } from 'vite-plugin-mock'
import Mock from "mockjs";
// 获取 mock.Random 对象
const Random = Mock.Random;

/** get请求
 * 获取用户列表
 */
let tableList:any[] = [];
tableList.push({
  id: Random.increment(1), // 获取全局唯一标识符
  userName: Random.name(),
  themeTitle: Random.ctitle(5,10),
  worksCount: Random.csentence(10,30),
  rowId: 1
})
tableList.push({
  id: Random.increment(1), // 获取全局唯一标识符
  userName: Random.name(),
  themeTitle: Random.ctitle(5,10),
  worksCount: Random.csentence(10,30),
  rowId: 2
})
tableList.push({
  id: Random.increment(1), // 获取全局唯一标识符
  userName: Random.name(),
  themeTitle: Random.ctitle(5,10),
  worksCount: Random.csentence(10,30),
  rowId: 3
})
for (let i = 0; i < 20; i++) {
  let newObject = {
    id: Random.increment(1), // 获取全局唯一标识符
    userName: Random.name(),
    rowId: i+1,
    themeTitle: Random.ctitle(5,10),
    worksCount: Random.csentence(10,30)
  };
  tableList.push(newObject);
}

let menuList:{} = {
  "appId": 12,
	"cardId": 122112,
	"appName": "学生总和素质评价",
	"appUrl": "/socialserv/index.html",
	"appIcon":"icon.png",
    menus:[
    {
        "menuName": "其他",
        "menuUrl": "/other",
        "subMenus": [
            // {
            //     "menuName": "基础信息",
            //     "menuUrl": "/sss/ssa",
            //     "subMenus": []
            // }
        ]
    },
    {
        "menuName": "表格",
        "menuUrl": "/table",
        "subMenus": [
            // {
            //     "menuName": "基础信息",
            //     "menuUrl": "/xxx/xss",
            //     "subMenus": []
            // }
        ]
    }
]}


const MockList: MockMethod[] = [
  {
    url: '/api/mockGetList',
    method: 'get',
    response: (req: Recordable) => {
      let code = 200; let message = '成功'
      return {
        code,
        message,
        data: tableList || []
      }
    }
  },
  {
    url:'/api/add',
    method: 'post',
    response: (req: Recordable) => {
      let newData = JSON.parse(req.body);
      newData.id = Random.increment(tableList.length+1);
      tableList.push(newData);
      return {
        code: "200",
        message: "success",
        data: tableList,
      };
    }
  },
  {
    url:'/api/menus',
    method: 'post',
    response: (req: Recordable) => {
      return {
        code: "200",
        message: "success",
        data: menuList,
      };
    }
  },
  {
    url:'/api/userInfo',
    method: 'get',
    response: (req: Recordable) => {
      return {
        code: "200",
        message: "success",
        data: menuList,
      };
    }
  },
  {
    url:'/api/login',
    method: 'post',
    response: (req: Recordable) => {
      const {userName,password} = req.body;
      let code = 100
      let message = ''
      let data = {}
      if(userName!=='admin') {
        code = 400
        message = '用户名错误'
      } else {
        if(password!=='12345678'){
          code = 400
          message = '密码错误'
        } else {
          code = 200
          message = 'success'
          data = {
            token: '234134141fdasfasf123434'
          }
        }
      }
      return {
        code,
        message,
        data: data,
      };
    }
  }
]

export default MockList;